package com.neutech.dao.impl;

import com.alibaba.fastjson.JSON;
import com.neutech.dao.SaleDAO;
import com.neutech.entity.Sale;
import com.neutech.util.DBUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class SaleDAOimpl  implements SaleDAO {
    @Override
    public List<Sale> findAll() {
        Connection conn = null;
        Statement stmt = null;
        String sql ="select id,name,qty from t_sale";
        ResultSet rs = null;
        List<Sale> sales = new ArrayList<>();
        try {
            conn = DBUtil.getConnection();
            stmt = DBUtil.getStatement(conn);
            rs = DBUtil.getResultSet(stmt,sql);
            while (rs.next()){
                Sale sale = new Sale();
                sale.setId(rs.getInt("id"));
                sale.setName(rs.getString("name"));
                sale.setQty(rs.getInt("qty"));
                sales.add(sale);

            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtil.close(rs,stmt,conn);

        }
        return sales;
    }

    public static void main(String[] args) {
        SaleDAO dao = new SaleDAOimpl();
        List<Sale> sales = dao.findAll();
      /*  for (Sale sale: sales){
            System.out.println(sale);
        }*/
        String strSales =  JSON.toJSONString(sales);
        System.out.println(strSales);
    }
}
